package com.see.domain;


public class Protocol {
	public static String TYPEMSG  = "typemsg=";
	public static String SUCCESS  = "success=";
	public static String PRODUCT  = "product=";
	public static String ERROR    = "error=";
	public static String USERNAME = "username=";
	public static String AMOUNT   = "amount=";
	public static String PRICE    = "price=";
	public static String ORDERTYPE= "ordertype=";
	public static String MARKET   = "market=";
	public static String ORDERID  = "orderid=";
	public static String ORDERMATCHPRICE   = "ordermatchprice=";
	public static String ORDERCOUNTERPARTY  = "ordercounterparty=";
	
	
	public static final String LOGIN_FIELD     = "login";
	public static final String ERROR_FIELD     = "error";
	public static final String SENDORDED_FIELD = "sendorder";
	public static final String FILLORDER_FIELD = "fillorder";
	public static final String CLOSE_FIELD     = "close";
	public static final String SEPARATOR       = "|";
	
	public static final String TRUE_FIELD      = "1";
	public static final String FALSE_FIELD     = "2";
	//errors
	public static final String ERROR_USERNAME    = "empty_user_name";
	public static final String ERROR_USEREXIST   = "user_exists";
	public static final String ERROR_USERNOTAUTH = "user_not_authenticat";
	public static final String ERROR_IN_FIELD    = "field_error_";
	public static final String ERROR_PRODUCT     = "no_such_";
	public static final String ERROR_PORT        = "system_or_zero_port";
	public static final String ERROR_SERVADRESS  = "empty_server_adress";

	
	public static String errormsg(String type, String error) {
		return TYPEMSG + type + SEPARATOR + SUCCESS + FALSE_FIELD  + SEPARATOR
				+ Protocol.ERROR + error + SEPARATOR;
	}

	public static String successmsg(String type, String extra) {
		return TYPEMSG + type + SEPARATOR + SUCCESS + TRUE_FIELD + SEPARATOR + extra;
	}

	public static String client_loginmsg(String userName){
		return TYPEMSG + LOGIN_FIELD + SEPARATOR + USERNAME + userName + SEPARATOR;
	}
	
	public static String client_sendorder(Order order){
		String str = TYPEMSG + SENDORDED_FIELD + SEPARATOR + 
				getOrderMainInfo(order);
		
		return str;
	}
	
	public static String server_sendorder(Order order){
		String str = TYPEMSG + FILLORDER_FIELD + SEPARATOR +
				getOrderMatchInfo(order) + 
				getOrderMainInfo(order);
		return str;
	}

	private static String getOrderMatchInfo(Order order){
		return ORDERID + order.getId() + SEPARATOR +
				ORDERMATCHPRICE + order.getMatchprice() + SEPARATOR +
				ORDERCOUNTERPARTY + order.getCounterparty() + SEPARATOR;
	}
	
	private static String getOrderMainInfo(Order order){
		String ordertype = ""; 
		if(order.getType() == Operation.BUY){ 
			ordertype = "1"; 
		} else {
			ordertype = "2"; 
		} 
		String market = ""; 
		if(order.getPrice().isMarket()){ 
			market = "1"; 
		} else {
			market = "2"; 
		} 
		
		return USERNAME + order.getUser() + SEPARATOR +
				
				PRODUCT + order.getProduct() + SEPARATOR +
				AMOUNT + order.getAmount() + SEPARATOR +
				PRICE + order.getPrice().getValue() + SEPARATOR+
				ORDERTYPE + ordertype + SEPARATOR+
				MARKET + market + SEPARATOR;
	}
	
}
